import request from "@/utils/request";
import { AxiosPromise } from "axios";
import moment from "moment";

import {
  TimeProjectPageQuery,
  TimeProjectPageResult,
  TimeProjectForm,
  TimeItemProjectForm,
  TimeItemProjectVO,
  TimeProjectPageVO,
} from "./types";

/**
 * 获取项目分页数据
 *
 * @param queryParams
 */
export function getTimeProjectPage(
  queryParams?: TimeProjectPageQuery
): AxiosPromise<TimeProjectPageResult> {
  return request({
    url: "/api/v1/time/project/page",
    method: "get",
    params: queryParams,
  });
}

/**
 * 获取项目表单数据
 *
 * @param id
 */
export function getTimeProjectForm(
  id: number
): AxiosPromise<TimeProjectPageVO> {
  return request({
    url: "/api/v1/time/project/" + id,
    method: "get",
  });
}

/**
 * 新增 时间管理选项列表
 */
export function addTimeProjectOptions(data: TimeItemProjectForm) {
  return request({
    url: "/api/v1/time/project/item",
    method: "post",
    data: data,
  });
}

/**
 * 修改 时间管理选项列表
 * @param id
 * @returns
 */
export function updateTimeProjectOptions(
  id: number,
  data: TimeItemProjectForm
) {
  return request({
    url: "/api/v1/time/project/item/" + id,
    method: "put",
    data: data,
  });
}
/**
 * 获取项目表单数据
 */
export function getTimeProjectOptions(
  id: number
): AxiosPromise<TimeItemProjectVO> {
  return request({
    url: "/api/v1/time/project/item/" + id,
    method: "get",
  });
}
/**
 * 新增项目
 *
 * @param data
 */
export function addTimeProject(data: TimeProjectForm) {
  if (data.beginTime != null) {
    data.beginTime = moment(data.beginTime).format("yyyy-MM-DD HH:mm:ss");
  }
  if (data.endTime != null) {
    data.endTime = moment(data.endTime).format("yyyy-MM-DD HH:mm:ss");
  }
  return request({
    url: "/api/v1/time/project",
    method: "post",
    data: data,
  });
}

/**
 * 修改项目
 *
 * @param id
 * @param data
 */
export function updateTimeProject(id: number, data: TimeProjectForm) {
  if (data.beginTime != null) {
    data.beginTime = moment(data.beginTime).format("yyyy-MM-DD HH:mm:ss");
  }
  if (data.endTime != null) {
    data.endTime = moment(data.endTime).format("yyyy-MM-DD HH:mm:ss");
  }
  return request({
    url: "/api/v1/time/project/" + id,
    method: "put",
    data: data,
  });
}

/**
 * 修改项目进度
 */
export function updateTimeProjectPercentage(
  id: number,
  data: TimeProjectPageVO
) {
  return request({
    url: "/api/v1/time/project/" + id + "/percentage",
    method: "put",
    data: data,
  });
}

/**
 * 修改项目占比
 */
export function updateTimeProjectProportion(
  id: number,
  data: TimeProjectPageVO
) {
  return request({
    url: "/api/v1/time/project/" + id + "/proportion",
    method: "put",
    data: data,
  });
}

/**
 * 删除项目
 */
export function deleteTimeProject(ids: string) {
  return request({
    url: "/api/v1/time/project/" + ids,
    method: "delete",
  });
}

export function deleteTimeProjectOptions(ids: string) {
  return request({
    url: "/api/v1/time/project/item/" + ids,
    method: "delete",
  });
}

export function getStatisticsPage(queryParams?: TimeProjectPageQuery) {
  return request({
    url: "/api/v1/time/project/statistics/page",
    method: "get",
    params: queryParams,
  });
}
